package com.ruoyi.site.service.impl;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.site.mapper.UserSubscriptionsMapper;
import com.ruoyi.site.domain.UserSubscriptions;
import com.ruoyi.site.service.IUserSubscriptionsService;

/**
 * 用户订阅Service业务层处理
 *
 * @author chuckeytang
 * @date 2025-10-13
 */
@Service
public class UserSubscriptionsServiceImpl implements IUserSubscriptionsService
{
    @Autowired
    private UserSubscriptionsMapper userSubscriptionsMapper;

    /**
     * 查询用户订阅
     */
    @Override
    public UserSubscriptions selectUserSubscriptionsBySubscriptionId(Long subscriptionId)
    {
        return userSubscriptionsMapper.selectUserSubscriptionsBySubscriptionId(subscriptionId);
    }

    /**
     * 查询用户当前的有效订阅 (用于鉴权)
     */
    @Override
    public UserSubscriptions selectActiveSubscriptionByUserId(Long userId) {
        return userSubscriptionsMapper.selectActiveSubscriptionByUserId(userId);
    }

    /**
     * 查询用户所有订阅列表
     */
    @Override
    public List<UserSubscriptions> selectUserSubscriptionsList(UserSubscriptions userSubscriptions)
    {
        return userSubscriptionsMapper.selectUserSubscriptionsList(userSubscriptions);
    }

    /**
     * 新增用户订阅
     */
    @Override
    public int insertUserSubscriptions(UserSubscriptions userSubscriptions)
    {
        // 自动设置时间戳
        if (userSubscriptions.getCreatedAt() == null) {
            userSubscriptions.setCreatedAt(new Date());
        }
        userSubscriptions.setUpdatedAt(new Date());
        return userSubscriptionsMapper.insertUserSubscriptions(userSubscriptions);
    }

    /**
     * 修改用户订阅
     */
    @Override
    public int updateUserSubscriptions(UserSubscriptions userSubscriptions)
    {
        // 自动更新时间戳
        userSubscriptions.setUpdatedAt(new Date());
        return userSubscriptionsMapper.updateUserSubscriptions(userSubscriptions);
    }

    /**
     * 批量删除用户订阅
     */
    @Override
    public int deleteUserSubscriptionsBySubscriptionIds(Long[] subscriptionIds)
    {
        return userSubscriptionsMapper.deleteUserSubscriptionsBySubscriptionIds(subscriptionIds);
    }

    /**
     * 删除用户订阅信息
     */
    @Override
    public int deleteUserSubscriptionsBySubscriptionId(Long subscriptionId)
    {
        return userSubscriptionsMapper.deleteUserSubscriptionsBySubscriptionId(subscriptionId);
    }
}